package com.pig4cloud.pig.module.controller.product;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.mer.MerCategory;
import com.pig4cloud.pig.module.api.entity.product.ProductType;
import com.pig4cloud.pig.module.service.mer.IMerCategoryService;
import com.pig4cloud.pig.module.service.mer.IMerService;
import com.pig4cloud.pig.module.service.product.IProductTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 商品分类Controller
 *
 * @author ruoyi
 * @date 2021-08-11
 */
@Validated
@Api(value = "商品分类控制器", tags = {"商品分类管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/product/type")
public class ProductTypeController {

    private final IProductTypeService productTypeService;
	private final IMerService merService;
	private final IMerCategoryService merCategoryService;

	@Value("${ruoyi.imgUrl}")
	private String imgUrl;

//	@Value("${ruoyi.imgUrlOss}")
//	private String imgUrlOss;

    /**
     * 查询商品分类列表
     */
    @ApiOperation("查询商品分类列表")
    @GetMapping("/list")
    public TableDataInfo<ProductType> list(@Validated ProductType productType) {
        QueryWrapper<ProductType> qw = new QueryWrapper<>();
		if(productType.getParentId()!=null){
			qw.like("parent_id",productType.getParentId());
		}
        if(StrUtil.isNotEmpty(productType.getTypeName())){
            qw.like("type_name",productType.getTypeName());
        }
        if(ObjectUtil.isNotEmpty(productType.getMerId())){
            qw.eq("mer_id",productType.getMerId());
        }
        qw.orderByAsc("sort");
		TableDataInfo<ProductType> td = PageUtils.buildDataInfo(productTypeService.page(PageUtils.buildPage(),qw));
		List<ProductType> list = td.getData();
		if(CollUtil.isNotEmpty(list)){
			list.forEach(type -> {
				if(StrUtil.isNotEmpty(type.getImgUrl()) && type.getImgUrl().indexOf("http") < 0){
					type.setImgPath(imgUrl + type.getImgUrl());
				}else{
					type.setImgPath(type.getImgUrl());
				}

				if(type.getParentId() > 0){
					QueryWrapper<MerCategory> qwPro = new QueryWrapper<>();
					qwPro.select("distinct mer_id");
					qwPro.eq("cate_child_id",type.getId());
//					qwPro.eq("state",0);
					long merCount = merCategoryService.count(qwPro);
					type.setMerCount(merCount);
				}
			});
		}
        return td;
    }

    /**
     * 获取商品分类详细信息
     */
    @ApiOperation("获取商品分类详细信息")
    @GetMapping("/{id}")
    public R<ProductType> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
    	ProductType type = productTypeService.getById(id);
    	if(type!=null){
			if(StrUtil.isNotEmpty(type.getImgUrl())){
				if(StrUtil.isNotEmpty(type.getImgUrl()) && type.getImgUrl().indexOf("http") < 0){
					type.setImgPath(imgUrl + type.getImgUrl());
				}else{
					type.setImgPath(type.getImgUrl());
				}
			}else{
				type.setImgPath("");
			}
		}
        return R.ok(type);
    }

	/**
	 * 商品分类列表
	 */
	@GetMapping("/treeSelect")
	@ApiOperation(value = "商品分类列表",notes = "商品分类列表")
	public R treeSelect(ProductType productType){
		List<ProductType> types = productTypeService.selectProductTypeList(productType);
		return R.ok(productTypeService.buildProductTypeTreeSelect(types));
	}

    /**
     * 商品分类列表
     */
    @GetMapping("/listAll")
    @ApiOperation(value = "商品分类列表",notes = "商品分类列表")
    public R listAll(ProductType productType){
        List<ProductType> types = productTypeService.selectProductTypeList(productType);
        return R.ok(types);
    }

    /**
     * 新增商品分类
     */
    @ApiOperation("新增商品分类")
    @SysLog("商品分类")

    @PostMapping()
    public R<Void> add(@Valid @RequestBody ProductType productType) {
        return R.okByRow(productTypeService.save(productType) ? 1 : 0);
    }

    /**
     * 修改商品分类
     */
    @ApiOperation("修改商品分类")
    @SysLog("商品分类")

    @PutMapping()
    public R<Void> edit(@Valid @RequestBody ProductType productType) {
        return R.okByRow(productTypeService.updateById(productType) ? 1 : 0);
    }

    /**
     * 删除商品分类
     */
    @ApiOperation("删除商品分类")
    @SysLog("商品分类")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] ids) {
        return R.okByRow(productTypeService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
